Namespace DataStream

    ''' <summary>
    ''' Provides a collection of Stream Items
    ''' </summary>
    ''' <remarks></remarks>
    <Serializable(), ObfuscationAttribute(Exclude:=True, ApplyToMembers:=True)> _
    Public Class Stream
        Inherits ObjectModel.KeyedCollection(Of UShort, StreamItem)

        Private mTimeStamp As DateTime
        Private mIndex As Integer = -1

        Sub New(Optional ByVal TimeStamp As DateTime = Nothing, Optional ByVal Index As Integer = -1)
            If TimeStamp = DateTime.MinValue Then
                mTimeStamp = DateTime.Now
            Else
                mTimeStamp = TimeStamp
            End If
            mIndex = Index
        End Sub

        ''' <summary>
        ''' Gets the timestamp of the current Stream
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property TimeStamp() As DateTime
            Get
                Return mTimeStamp
            End Get
        End Property

        ''' <summary>
        ''' Gets the index of the current stream
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Index() As Integer
            Get
                Return mIndex
            End Get
            Set(ByVal value As Integer)
                mIndex = value
            End Set
        End Property

        Public Function GetDisplayOrDefault(ByVal Key As String) As String
            If Me.Contains(Key) Then
                Dim item As StreamItem = Me(Key)
                Return item.FormatedValue
            Else
                Return "n/a"
            End If
        End Function

        Public Sub setTimeStamp(ByVal value As DateTime)
            mTimeStamp = value

            ' Force the update of items
            For Each item As StreamItem In Me
                item.TimeStamp = value
            Next
        End Sub

#Region "Collection Functions"
        Protected Overrides Function GetKeyForItem(ByVal item As StreamItem) As UShort
            Return item.NameID
        End Function

        ''' <summary>
        ''' Gets a stream item by its key value
        ''' </summary>
        ''' <param name="key"></param>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Default Shadows ReadOnly Property Item(ByVal key As String) As StreamItem
            Get
                If StreamItem.NameDictionary.Contains(key) Then
                    Dim keyID As UShort = StreamItem.NameDictionary.Item(key)

                    If MyBase.Contains(keyID) Then
                        Dim stItem As StreamItem = MyBase.Item(keyID)
                        stItem.TimeStamp = mTimeStamp
                        stItem.Index = mIndex
                        Return stItem
                    End If
                End If
                Return Nothing
            End Get
        End Property

        ''' <summary>
        ''' Gets or Sets a stream item by its index
        ''' </summary>
        ''' <param name="index"></param>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Default Shadows Property Item(ByVal index As Integer) As StreamItem
            Get
                Dim stItem As StreamItem = MyBase.Item(index)
                stItem.TimeStamp = mTimeStamp
                stItem.Index = mIndex
                Return stItem
            End Get
            Set(ByVal value As StreamItem)
                MyBase.Item(index) = value
            End Set
        End Property

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="key"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Overloads Function contains(ByVal key As String) As Boolean
            Dim keyID As UShort
            If StreamItem.NameDictionary.Contains(key) Then
                keyID = StreamItem.NameDictionary.Item(key)

                If MyBase.Contains(keyID) Then
                    Return True
                Else
                    Return False
                End If
            Else
                Return False
            End If

        End Function
#End Region

    End Class

End Namespace